package com.wenwei.sharebox.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.bean.GoodsInfoBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/07/09 0009.
 */
public class ProvideCheckActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.check_ok)
    TextView checkOk;
    @Bind(R.id.check_error)
    TextView checkError;
    @Bind(R.id.check_error_et)
    EditText checkErrorEt;
    @Bind(R.id.check_commit)
    TextView checkCommit;

    private String deviceNum;
    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_provide_check;
    }

    @Override
    protected void init() {
        super.init();
        deviceNum = getIntent().getStringExtra("deviceCode");
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("开柜检查");
    }

    @Override
    protected void initData() {
        super.initData();
        getGoodsMsg();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.base_back, R.id.check_ok, R.id.check_error, R.id.check_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.check_ok:
                checkType = 1;
                break;
            case R.id.check_error:
                checkType = 0;
                break;
            case R.id.check_commit:
                shopInspection();
                break;
        }
    }

    private int goodsId;
    private int checkType = -1;
    //获取商品详情
    BaseSubscriber<GoodsInfoBean> goodsMsgSubscriber;
    private void getGoodsMsg(){
        goodsMsgSubscriber = new BaseSubscriber<GoodsInfoBean>(mContext) {
            @Override
            public void onSuccess(GoodsInfoBean goodsInfoBean) {
                goodsId = goodsInfoBean.getId();
            }
        };
        try{
            JSONObject jb = new JSONObject();
            jb.put("device_unique_num",deviceNum);
            ApiManager.getInstance().checkDevice(goodsMsgSubscriber,deviceNum);
        }catch(Exception e){}
    }

    //商品检测
    BaseSubscriber<GoodsInfoBean> shopInspectionSubscriber;
    private void shopInspection(){
        shopInspectionSubscriber = new BaseSubscriber<GoodsInfoBean>(mContext) {
            @Override
            public void onSuccess(GoodsInfoBean goodsInfoBean) {
                showToast("提交成功");
                finish();
            }
        };
        ApiManager.getInstance().shopInspection(shopInspectionSubscriber,goodsId,checkType,checkErrorEt.getText().toString());
    }
}
